<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Gate;
use App\User;
use App\Http\Requests\UserCreateRequest;
use App\Http\Requests\UserUpdateRequest;
use Auth;

use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * Specify Repository class.
     *
     * @return App\Repositories
     */
    public function __construct(UserRepository $user){
        $this->userRepo = $user;
    }

    /**
     * Display the page dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function dashboard()
    {
        return view('admin.user.index');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request){
        $collection = $this->userRepo->usersWithRoles();
        $result['draw']            = $request->input('draw', 0);
        $result['recordsTotal']    = $collection->total();
        $result['recordsFiltered'] = $result['recordsTotal'];
        $result['data']            = $collection->items();
        $result['error']           = '';

        return response()->json($result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserCreateRequest $request)
    {
        $result = ['code'=>0, 'msg'=>''];
        $data = $this->userRepo->store($request->all());
        if ($data == false) {
            $result = ['code'=>1, 'msg'=>'操作失败！'];
        }

        return response()->json($result);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     * 更新后被更新用户需要重新登录才生效。
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserUpdateRequest $request, $id)
    {
        $user = User::findOrFail($id);

        $result = ['code'=>0, 'msg'=>''];
        $data = $this->userRepo->update($request->all(), $user);
        if ($data == false) {
            $result = ['code'=>1, 'msg'=>'操作失败！'];
        }
        
        return response()->json($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user = User::findOrFail($id);
        if (Gate::denies('destroy', $user)) {
            abort(403, 'Forbidden');
        }

        $result = ['code'=>0, 'msg'=>''];
        $data = $this->userRepo->delete($user, $id);
        if ($data == false) {
            $result = ['code'=>1, 'msg'=>'操作失败！'];
        }
        
        return response()->json($result);
    }

    public function profile(Request $request)
    {
        $user = $request->user();
        echo '<pre>';
        var_dump($user);
        echo '</pre>';
    }

    public function updateProfile(Request $request)
    {
        if ($request->user()) {
            // $request->user() 返回认证用户实例...
        }
    }

    public function test(){
        if (Auth::check()) {
            echo 'The user is logged in...';
        } else {
            echo 'The user not logged in...';
        }
    }
}
